//
//  PPCreateAlbumTableViewTitleCell.m
//  pickpic
//
//  Created by Jinxiao on 9/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentTableViewTitleCell.h"

#import "PPAlbumContentViewDataSource.h"

@interface PPAlbumContentTableViewTitleCell () <UITextFieldDelegate>

@end

@implementation PPAlbumContentTableViewTitleCell

- (void)awakeFromNib {
    [super awakeFromNib];

    _titleField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"图册标题" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRGBHex:0xb2b2b2], NSFontAttributeName : [UIFont systemFontOfSize:16]}];
    _titleField.returnKeyType = UIReturnKeyDone;

    _titleField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChanged) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setDataSource:(PPAlbumContentViewDataSource *)dataSource {
    _dataSource = dataSource;

    _titleField.text = dataSource.title;
}

- (void)textFieldDidChanged
{
    _dataSource.title = _titleField.text;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];

        return NO;
    }

    return YES;
}

@end
